package com.example.finalhomework.cardgame.ui

import android.content.Context
import android.content.SharedPreferences
import android.net.Uri
import android.util.Log

// 定义用户信息数据类
data class UserInfo(
    val name: String, // 用户名
    val gender: String, // 性别
    val studentId: String, // 学号
    val phone: String, // 电话
    val email: String, // 邮箱
    val hobbies: String, // 爱好
    val avatarUri: String?, // 头像URI
    val highestScore: Int, // 最高分数
    val advancedHighestScore: Int // 高级最高分数
)

// 从SharedPreferences加载用户信息（显示信息时调用）
fun loadUserInfo(context: Context): UserInfo {
    // 获取SharedPreferences实例
    val sharedPreferences: SharedPreferences = context.getSharedPreferences("user_info", Context.MODE_PRIVATE)
    // 默认头像资源ID
//    val defaultAvatarResId = R.drawable.avatar
    val defaultAvatarUri = Uri.parse("android.resource://${context.packageName}/drawable/avatar")
    // 返回UserInfo对象，包含从SharedPreferences加载的数据

    // 检查是否存在非默认值的最高分数
    val highestScore = sharedPreferences.getInt("highestScore", 0)
    val advancedHighestScore = sharedPreferences.getInt("advancedHighestScore", 0)

    val userInfo = UserInfo(
        sharedPreferences.getString("name", "杨雪琴") ?: "杨雪琴", // 加载用户名，默认值为"杨雪琴"
        sharedPreferences.getString("gender", "Female") ?: "Female", // 加载性别，默认值为"Female"
        sharedPreferences.getString("studentId", "2022110650") ?: "2022110650", // 加载学号，默认值为"2022110650"
        sharedPreferences.getString("phone", "11012341234") ?: "11012341234", // 加载电话，默认值为"11012341234"
        sharedPreferences.getString("email", "11234567890@qq.com") ?: "11234567890@qq.com", // 加载邮箱，默认值为"11234567890@qq.com"
        sharedPreferences.getString("hobbies", "Music") ?: "Music", // 加载爱好，默认值为"Music"
        sharedPreferences.getString("avatarUri", defaultAvatarUri.toString()), // Use default avatar URI if null
        if (highestScore != 0) highestScore else 0, // 加载已有最高分数
        if (advancedHighestScore != 0) advancedHighestScore else 0 // 加载已有高级最高分数
    )

    Log.d("loadUserInfo", "Loaded user info: $userInfo")
    return userInfo

}

// 将用户信息保存到SharedPreferences(修改信息时调用)
fun saveUserInfo(context: Context, userInfo: UserInfo) {
    // 获取SharedPreferences实例
    val sharedPreferences: SharedPreferences = context.getSharedPreferences("user_info", Context.MODE_PRIVATE)
    // 使用SharedPreferences.Editor保存用户信息
    // 获取当前保存的最高分数
    val currentHighestScore = sharedPreferences.getInt("highestScore", 0)
    val currentAdvancedHighestScore = sharedPreferences.getInt("advancedHighestScore", 0)

    // 打印日志信息，检测最高分数的变化
//    Log.d("saveUserInfo", "Current highest score: $currentHighestScore, New highest score: ${userInfo.highestScore}")
//    Log.d("saveUserInfo", "Current advanced highest score: $currentAdvancedHighestScore, New advanced highest score: ${userInfo.advancedHighestScore}")
    // 调用 updateHighestScore 更新最高分
    updateHighestScore(sharedPreferences, userInfo.highestScore)
    updateAdvancedHighestScore(sharedPreferences, userInfo.advancedHighestScore)
    //
//    Log.d("saveUserInfo", "Current highest score: $currentHighestScore, New highest score: ${userInfo.highestScore}")
//    Log.d("saveUserInfo", "Current advanced highest score: $currentAdvancedHighestScore, New advanced highest score: ${userInfo.advancedHighestScore}")

    with(sharedPreferences.edit()) {
        putString("name", userInfo.name) // 保存用户名
        putString("gender", userInfo.gender) // 保存性别
        putString("studentId", userInfo.studentId) // 保存学号
        putString("phone", userInfo.phone) // 保存电话
        putString("email", userInfo.email) // 保存邮箱
        putString("hobbies", userInfo.hobbies) // 保存爱好
        putString("avatarUri", userInfo.avatarUri) // 保存头像URI
        apply() // 应用更改
    }
    Log.d("saveUserInfo", "User info saved: $userInfo")
}

fun updateHighestScore(sharedPreferences: SharedPreferences, newHighestScore: Int) {
    val currentHighestScore = sharedPreferences.getInt("highestScore", 0)
    if (newHighestScore > currentHighestScore) {
        Log.d("updateHighestScore", "Updating highest score from $currentHighestScore to $newHighestScore")
        with(sharedPreferences.edit()) {
            putInt("highestScore", newHighestScore)
            apply()
        }
    }
}

fun updateAdvancedHighestScore(sharedPreferences: SharedPreferences, newAdvancedHighestScore: Int) {
    val currentAdvancedHighestScore = sharedPreferences.getInt("advancedHighestScore", 0)
    if (newAdvancedHighestScore > currentAdvancedHighestScore) {
        Log.d("updateAdvancedHighestScore", "Updating advanced highest score from $currentAdvancedHighestScore to $newAdvancedHighestScore")
        with(sharedPreferences.edit()) {
            putInt("advancedHighestScore", newAdvancedHighestScore)
            apply()
        }
    }
}
